package data
{
	import control.manager.DataManager;
	import control.manager.LayerManager;
	import control.manager.MapManager;
	import control.manager.RoleManager;
	
	import flash.geom.Point;
	
	import org.osmf.net.StreamingURLResource;
	
	import view.item.LevelupEffect;
	import view.layer.GameLayer;
	import view.role.MapPlayer;

	/**
	 * 玩家数据 
	 * @author leo
	 * 
	 */	
	public class PlayerData
	{
		/**
		 * 角色名字 
		 */		
		public var roleName :String;
		/**
		 * 角色类型 
		 */		
		public var roleType :String;
		/**
		 * 角色图片链接 
		 */		
		public var roleURL :String;
		
		/**
		 * 玩家id 
		 */		
		public var playerID :String;
		/**
		 * 玩家行走速度 
		 */		
		public var playerSpeed :int;
		/**
		 * 玩家血量 
		 */		
		public var playerHP :int;
		/**
		 * 玩家攻击力 
		 */		
		public var playerATT :int;
		/**
		 * 玩家等级 
		 */		
		public var playerLevel :int;
		/**
		 * 玩家经验 
		 */		
		public var playerExp :int;
		/**
		 * 玩家金币 
		 */		
		public var playerGold :int;
		/**
		 *  角色有的动作数组 例如 行走，待机，攻击==
		 */		
		public var action :Array;
		/**
		 * 玩家当前所在的地图id 
		 */		
		public var curMapID :int;
		/**
		 * 玩家出生地点 
		 */		
		public var bornPoint :Point;
		/**
		 * 玩家的攻击范围 
		 */		
		public var playerATTRange :int;
		/**
		 * 玩家性别   1为男性  2为女性
		 */		
		public var sex :int;
		/**
		 * 玩家职业  1猛将    2刺客    3谋士
		 */		
		public var profession :int;
		/**
		 * 正在任务中的任务列表 
		 */		
		public var taskingList :Array;
		/**
		 * 可接任务列表 
		 */		
		public var canAcceptTaskList :Array;
		/**
		 * 已经完成的任务列表 
		 */		
		public var completeList :Array;
		public function PlayerData()
		{
		}
		/**
		 * 任务完成后更新资产 
		 * @param userTaskVO
		 * 
		 */		
		public function updateAssetsByTask(userTaskVO :UserTaskVO):void
		{
			if(userTaskVO && userTaskVO.taskVO)
			{
				if(userTaskVO.taskVO.rewardExp > 0)
				{
					this.playerExp += userTaskVO.taskVO.rewardExp;
				}
				
				if(userTaskVO.taskVO.rewardGold > 0)
				{
					this.playerGold += userTaskVO.taskVO.rewardGold;
				}
			}
			
			var level :int = DataManager.I.curLevel(this.playerExp);
			
			if(level > this.playerLevel)
			{
				this.playerLevel = level;
				//触发升级
				
				var gameLayer :GameLayer = LayerManager.instance.getLayer(GameLayer.NAME) as GameLayer;
				var player :MapPlayer = gameLayer.player;
				if(player)
				{
					var levelupEffect :LevelupEffect = new LevelupEffect();
					levelupEffect.x = player.position.x;
					levelupEffect.y = player.position.y;
					gameLayer.roleLayer.addChild(levelupEffect);
				}
			}
			
			
			updatePlayerDisplayData();
		}
		
		public function updatePlayerDisplayData():void
		{
			RoleManager.instance.setRoleUIDisplay();
		}
		/**
		 * 增加进行中的任务 
		 * @param taskVO
		 * 
		 */		
		public function addTasking(taskVO :TaskVO):void
		{
			if(!taskVO)
			{
				trace("taskVO 为空 at addTask");
				return;
			}
			var userTaskVO :UserTaskVO = new UserTaskVO();
			userTaskVO.taskID = taskVO.taskID;
			userTaskVO.isComplete = false;
			userTaskVO.isTasking = true;
			userTaskVO.canComplete = false;
			userTaskVO.lastTaskID = taskVO.lastTaskID;
			userTaskVO.netxTaskID = taskVO.nextTaskID;
			userTaskVO.sendID = taskVO.sendNPCID;
			userTaskVO.completeID = taskVO.completeNPCID;
			userTaskVO.taskType = taskVO.taskType;
			userTaskVO.taskSort = taskVO.taskSort;
			
			if(taskVO.battleTaskData.length > 0)
			{
				userTaskVO.battleType = taskVO.battleTaskData[0];
				userTaskVO.battleObjID = taskVO.battleTaskData[1];
				userTaskVO.battleNum = 0;
			}
			
			taskingList.push(userTaskVO);
		}
		
		/**
		 * 增加完成的任务 
		 * @param userTaskVO
		 * 
		 */		
		public function addCompleteTask(userTaskVO :UserTaskVO):void
		{
			for(var index :int = taskingList.length - 1; index >= 0 ; index--)
			{
				var item :UserTaskVO = taskingList[index] as UserTaskVO;
				
				if(!item) continue;
				if(item.taskID == userTaskVO.taskID)
				{
					taskingList.splice(index,1);
				}
			}
			completeList.push(userTaskVO);
		}
		
		/**
		 * 是否能接受该任务 
		 * @param taskVO
		 * @return 
		 * 
		 */		
		public function canDo(taskVO :TaskVO):Boolean
		{
			var result :Boolean = true;
			var userTaskVO :UserTaskVO;
			for each(userTaskVO in this.taskingList)
			{
				if(userTaskVO.taskID == taskVO.taskID)
				{
					result = false;
					return result;
				}
			}
			
			for each(userTaskVO in this.completeList)
			{
				if(userTaskVO.taskID == taskVO.taskID)
				{
					result = false;
					return result;
				}
			}
			
			return result;
		}
		
		public static function createPlayerData(xml :XML):PlayerData
		{
			var playerData :PlayerData
			if(xml.playerData)
			{
				var item :XMLList = xml.playerData;
				playerData = new PlayerData();
				playerData.action = MapManager.instance.string2ActionAry(item.@action);
				playerData.playerATT = item.@playerATT;
				playerData.playerHP = item.@playerATT;
				playerData.playerID = item.@playerID;
				playerData.roleName = item.@roleName;
				playerData.playerSpeed = item.@playerSpeed;
				playerData.roleType = item.@roleType;
				playerData.roleURL = item.@roleURL;
				playerData.playerLevel = item.@playerLevel;
				playerData.playerExp = item.@playerExp;
				playerData.playerGold = item.@playerGold;
				playerData.curMapID = item.@curMapID;
				playerData.bornPoint = MapManager.instance.string2Point(item.@bornPoint);
				playerData.playerATTRange = item.@playerATTRange;
				playerData.taskingList = [];
				playerData.completeList = [];
			}
			return playerData;
		}
		
		public static function changeData2PlayerData(obj :Object):PlayerData
		{
			
			if(obj)
			{
				var pd :PlayerData = new PlayerData();
				pd.action = obj.action;
				var str :String = obj.bornPoint.x +"," + obj.bornPoint.y;
				pd.bornPoint = MapManager.instance.string2Point(str);
				pd.curMapID = obj.curMapID;
				pd.playerATT = obj.playerATT;
				pd.playerATTRange = obj.playerATTRange;
				pd.playerExp = obj.playerExp;
				pd.playerGold = obj.playerGold;
				pd.playerHP = obj.playerHP;
				pd.playerID = obj.playerID;
				pd.playerLevel = obj.playerLevel;
				pd.playerSpeed = obj.playerSpeed;
				pd.profession = obj.profession;
				pd.roleName = obj.roleName;
				pd.roleType = obj.roleType;
				pd.roleURL = obj.roleURL;
				pd.sex = obj.sex;
				pd.taskingList = changeObj2UserTaskVOList(obj.taskingList);
				pd.completeList = changeObj2UserTaskVOList(obj.completeList);
				
				return pd;
			}
			
			return null;
		}
		/**
		 * 将存储在本地的对象转换成用户的任务信息列表 
		 * @param arr
		 * @return 
		 * 
		 */		
		private static function changeObj2UserTaskVOList(arr :Array):Array
		{
			var result :Array = [];
			for each(var obj :Object in arr)
			{
				var userTaskVO :UserTaskVO = new UserTaskVO();
				userTaskVO.taskID = obj.taskID;
				userTaskVO.isComplete = obj.isComplete;
				userTaskVO.isTasking = obj.isTasking;
				userTaskVO.lastTaskID = obj.lastTaskID;
				userTaskVO.netxTaskID = obj.netxTaskID;
				userTaskVO.sendID = obj.sendID;
				userTaskVO.completeID = obj.completeID;
				userTaskVO.canComplete = obj.canComplete;
				userTaskVO.taskType = obj.taskType;
				userTaskVO.taskSort = obj.taskSort;
				
//				if(obj.battleTaskData.length > 0)
//				{
//					userTaskVO.battleType = obj.battleTaskData[0];
//					userTaskVO.battleObjID = obj.battleTaskData[1];
//					userTaskVO.battleNum = obj.battleTaskData[2];
//				}
				
				result.push(userTaskVO);
			}
			
			if(result.length == 0)
			{
				trace("转换个人任务信息失败 at PlayerData");
			}
			return result;
		}
		
	}
}